package com.bins.dentist.clinic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bins.dentist.clinic.constants.CommonConstants;
import com.bins.dentist.clinic.entity.AreaEntity;
import com.bins.dentist.clinic.enums.AreaStatusEnum;
import com.bins.dentist.clinic.mapper.AreaMapper;
import com.bins.dentist.clinic.model.bo.AreaBo;
import com.bins.dentist.clinic.model.dto.AreaDto;
import com.bins.dentist.clinic.model.vo.AreaVo;
import com.bins.dentist.clinic.service.AreaService;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 区域表 服务实现类
 * </p>
 *
 * @author bins
 * @since 2023-04-23 13:22:36
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, AreaEntity> implements AreaService {

    @Override
    public List<AreaVo> selectList(AreaDto dto) {
        AreaBo areaBo = new AreaBo();
        BeanUtils.copyProperties(dto, areaBo);
        if (ObjectUtils.isEmpty(dto.getAreaCode())) {
            areaBo.setAreaCode(CommonConstants.DEFAULT_AREA_CODE);
        }
        if (ObjectUtils.isEmpty(dto.getAreaLevel())) {
            areaBo.setAreaLevel(CommonConstants.DEFAULT_AREA_LEVEL);
        }
        List<AreaEntity> list = queryAreaList(areaBo);
        return transferAreaList(list);
    }

    private List<AreaVo> transferAreaList(List<AreaEntity> list) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<AreaVo> resultList = Lists.newArrayListWithCapacity(list.size());
        list.forEach(a -> {
            AreaVo vo = new AreaVo();
            BeanUtils.copyProperties(a, vo);
            resultList.add(vo);
        });
        return resultList;
    }

    private List<AreaEntity> queryAreaList(AreaBo areaBo) {
        LambdaQueryWrapper<AreaEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ObjectUtils.isNotEmpty(areaBo.getAreaCode()), AreaEntity::getAreaCode, areaBo.getAreaCode())
                .eq(ObjectUtils.isNotEmpty(areaBo.getAreaLevel()), AreaEntity::getAreaLevel, areaBo.getAreaLevel())
                .eq(AreaEntity::getAreaStatus, AreaStatusEnum.NORMAL.getStatus());
        return this.baseMapper.selectList(queryWrapper);
    }
}
